#!/bin/bash
########################################################################
# script name:  update_baconf.sh
# Used for:
# Author:       Martin Dumont
# Contributors:
# Version:      0.1
#
# (date format: date +'%Y/%m/%d')
#
# Date          Version Contributors    Modifications
# ----------    ------- ------------    -------------
# 2011/10/05    0.1     MDUMONT         initial
########################################################################
usage()
{
cat << STOP

usage: $0 [options] [ARG]

This script is used for ....

OPTIONS:
   -h               Host [ALL]
...

Ex.:
$0 -h saphrt01
$0 -h ALL
STOP
}
#######################################
# Get parameters
#######################################
while getopts a:h:c OPTION
do
        case $OPTION in
        h)
                HOSTS=$(echo $OPTARG | sed 's/,/ /g' | sed 's/^-*//' | cut -d. -f1 | tr '[A-Z]' '[a-z]')
                ;;
        \?)
                usage
                exit 1
                ;;
        *)
                usage
                exit 1
                ;;
        esac
done
if [ $OPTIND = 1 ]; then usage; exit 1; fi
#######################################
# Variables and environnement
#######################################
BAENV=$(dirname `which $0`)/loadenv.sh
if [ -f $BAENV ]; then . $BAENV; else echo "No env library ($BAENV)"; exit 1; fi
SSH_OPTS=" -x -q -o ConnectTimeout=15 -o BatchMode=yes -o StrictHostKeyChecking=no -o LogLevel=fatal"
#######################################
# MAIN
#######################################
NEWHOSTLIST=$(checkhost $HOSTS | grep -v Verifying)
for HOSTNAME in $NEWHOSTLIST
do
	SYS_NAME=$(list_sys_by_host $HOSTNAME)
	LAND=$(if [ "$HOSTNAME" = "localhost" ]; then list_land_by_host `uname -n`; else list_land_by_host $HOSTNAME; fi)
	echo -n "Updating /etc/BA.conf file on $HOSTNAME: "
	cat <<-EOF | if [ "$HOSTNAME" != "localhost" ]; then sudo su - saunix -c "ssh $SSH_OPTS $HOSTNAME \"cat | sudo /bin/bash\""; else cd /tmp; cat | sudo /bin/bash; fi
		(
		echo "LAND=$LAND"
		echo "#SYS_NAME=$SYS_NAME"
		) > /etc/BA.conf && echo ok
		chmod 755 /etc/BA.conf
	EOF
done
#######################################
exit 0

